package blog.controller;

import blog.entity.Tag;
import blog.service.TagService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/tag")
public class TagController {
    @Resource
    private TagService tagService;
    @RequestMapping("")
    public String tagList(Map map){
        List<Tag> tagList =  tagService.findAllTag();
        map.put("tagList",tagList);
        return "tag/tag-list";
    }
    @RequestMapping("/delete/{id}")
    public String deleteTag(@PathVariable("id") Integer id,Map map){
        tagService.deleteTagById(id);
        map.put("msg","删除成功");
        return "forward:/tag";
    }
    @RequestMapping("/insertSubmit")
    public String insertTag(@ModelAttribute("tagName") String tagName,@ModelAttribute("tagDescription") String tagDescription,Map map)
    {
        tagService.addTag(tagName,tagDescription);
        map.put("msg","添加成功");
        return "forward:/tag";
    }
    @RequestMapping("/validateTag") @ResponseBody
    public String validateTag(String name) {
       Tag tag =  tagService.selectTagByName(name);
       return tag!=null?"-1":"1";  //-1表示该标签名已存在,1表示不存在
    }
    @RequestMapping("/edit/{id}")
    public String gotoEdit(@PathVariable("id") Integer id,Map map){
        Tag tag = tagService.getTagById(id);
        map.put("tag",tag);
        return "tag/tag-edit";
    }
    @RequestMapping("/updateTag")
    public String edit(Tag tag,Map map){
        tagService.updateTag(tag);
        return "forward:/tag";
    }
}
